package org.devcp.tp.common.websocket.model;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import org.devcp.tp.common.event.SysEvent;
import org.devcp.tp.common.event.SysEventRegister;
import org.devcp.tp.common.event.listner.SysEventListener;
import org.devcp.tp.common.event.util.SysEventUtil;
import org.devcp.tp.common.websocket.WebsocketSessionUtil;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import com.alibaba.fastjson.JSONObject;

public class WebsocketSessionWrapper {

	
	private WebSocketSession session;
	private static final String  ROOM_SPEEK_REGISTER_KEY = "roomSpeekRegisterKey";
	private static final String  ROOM_BASE_INFO_REGISTER_KEY = "roomBaseInfoRegisterKey";
	private static final String  ROOM_VOTING_REGISTER_KEY = "roomVotingRegisterKey";
	private static final String NOTIFY_CREATED_KEY = "notifyCreatedKey";
	private static final String NOTIFY_UPDATED_KEY = "notifyUpdatedKey";
	private static final String NOTIFY_DELECTED_KEY = "notifyDelectedKey";
	private static final String NOTIFY_FREEZED_KEY = "notifyFreezedKey";
	private static final String NOTIFY_UNFREEZED_KEY = "notifyUnfreezedKey";
	private static final String ROOM_SPEEK_MSG_DELETED_KE = "roomSpeekMsgDeletedKey";
	
	private static final String ROOM_SPEEKED = "roomSpeeked";
	private static final String ROOM_SPEEK_MSG_DELETED = "roomSpeekMsgDeleted";
	private static final String ROOM_VOTED = "roomVoted";
	private static final String ROOM_BASE_INFO_UPDATE = "roomBaseInfoUpdate";
	private static final String NOTIFY_CREATED = "notifyCreated";
	private static final String NOTIFY_UPDATED = "notifyUpdated";
	private static final String NOTIFY_DELECTED = "notifyDelected";
	private static final String NOTIFY_FREEZED = "notifyFreezed";
	private static final String NOTIFY_UNFREEZED = "notifyUnfreezed";
	
	public WebsocketSessionWrapper(){
		
	}
	public WebsocketSessionWrapper(WebSocketSession session){
		this.session = session;
	}
	
	/**
	 * 为websocket session添加房间喊话事件监听器，当系统有消息事件时，可以通过该监听器调用session向前端页面推送消息
	 * @param listener
	 */
	public void addRoomSpeekListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(ROOM_SPEEK_REGISTER_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(ROOM_SPEEKED), listener ));
	}
	/**
	 * 房间喊话信息删除事件监听器
	 * @param listener
	 */
	public void addRoomSpeekMsgDeletedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(ROOM_SPEEK_MSG_DELETED_KE, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(ROOM_SPEEK_MSG_DELETED), listener ));
	}
	/**
	 * 添加房间基本信息更新监听器
	 * @param listener
	 */
	public void addRoomBaseInfoUpdateListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(ROOM_BASE_INFO_REGISTER_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(ROOM_BASE_INFO_UPDATE), listener ));
	}
	
	/**
	 * 添加房间投票动作监听器
	 * @param listener
	 */
	public void addRoomVotingListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(ROOM_VOTING_REGISTER_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(ROOM_VOTED), listener ));
	}
	/**
	 * 喊单信息创建事件监听器
	 * @param listener
	 */
	public void addNotifyCreatedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(NOTIFY_CREATED_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(NOTIFY_CREATED), listener ));
	}
	/**
	 * 喊单消息更新事件监听器
	 * @param listener
	 */
	public void addNotifyUpdatedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(NOTIFY_UPDATED_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(NOTIFY_UPDATED), listener ));
	}
	/**
	 * 喊单消息删除
	 * @param listener
	 */
	public void addNotifyDelectedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(NOTIFY_DELECTED_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(NOTIFY_DELECTED), listener ));
	}
	/**
	 * 喊单消息禁用
	 * @param listener
	 */
	public void addNotifyFreezedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(NOTIFY_FREEZED_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(NOTIFY_FREEZED), listener ));
	}
	/**
	 * 喊单消息启用
	 * @param listener
	 */
	public void addNotifyUnFreezedListener(SysEventListener listener){
		String uuid = UUID.randomUUID().toString();
		session.getAttributes().put(NOTIFY_UNFREEZED_KEY, uuid);
		SysEventUtil.registerMap.put(uuid,new SysEventRegister(session.getId(),new SysEvent(NOTIFY_UNFREEZED), listener ));
	}
	
	
	public static void removeRegisterMap(WebSocketSession session){
		String k1 = (String)session.getAttributes().get(ROOM_SPEEK_REGISTER_KEY);
		String k2 = (String)session.getAttributes().get(ROOM_BASE_INFO_REGISTER_KEY);
		String k3 = (String)session.getAttributes().get(ROOM_VOTING_REGISTER_KEY);
    	SysEventUtil.registerMap.remove(k1);
    	SysEventUtil.registerMap.remove(k2);
    	SysEventUtil.registerMap.remove(k3);
	}
	
	public WebSocketSession getSession() {
		return session;
	}
	public void setSession(WebSocketSession session) {
		this.session = session;
	}
}
